package de.hauschild.gmltracer.tracer.impl.tier1;

import java.util.Comparator;

import de.hauschild.gmltracer.tracer.Intersection;

/**
 * Compares {@link Intersection} by distance of {@link Intersection#getPoint()} to coordinate system origin. Nearer
 * first.
 * 
 * @since 1.0.0
 * @author Klaus Hauschild
 */
public class IntersectionDistanceComparator implements Comparator<Intersection> {

  /**
   * Instantiates a new {@link IntersectionDistanceComparator}.
   */
  public IntersectionDistanceComparator() {
  }

  @Override
  public int compare(final Intersection firstIntersection, final Intersection secondIntersection) {
    final double firstSquareLength = firstIntersection.getPoint().getX() * firstIntersection.getPoint().getX()
        + firstIntersection.getPoint().getY() * firstIntersection.getPoint().getY()
        + firstIntersection.getPoint().getZ() * firstIntersection.getPoint().getZ();
    final double secondSquareLength = secondIntersection.getPoint().getX() * secondIntersection.getPoint().getX()
        + secondIntersection.getPoint().getY() * secondIntersection.getPoint().getY()
        + secondIntersection.getPoint().getZ() * secondIntersection.getPoint().getZ();
    return (int) (firstSquareLength - secondSquareLength);
  }

}
